package types

import (
	"time"
	"todo-list/repository/db/model"
)

type ShowTaskReq struct {
	ID uint `json:"id" form:"id" uri:"id" binding:"required"`
}

type ListTaskReq struct {
	PagedReq
	Status      *model.TaskStatus `json:"status" form:"status" uri:"status"`
	Text        *string           `json:"text" form:"text" uri:"text"`
	PublicOnly  bool              `json:"publicOnly" form:"publicOnly" uri:"publicOnly"`
	CreateAt    *int64            `json:"createAt" form:"createAt" uri:"createAt"`
	NeedContent bool              `json:"needContent" form:"needContent" uri:"needContent"`
}

type UpdateTaskReq struct {
	ID      uint             `json:"id" form:"id" uri:"id" binding:"required"`
	Title   string           `json:"title" binding:"max=128"`
	Content string           `json:"content"`
	Status  model.TaskStatus `json:"status"`
	Public  *bool            `json:"public"`
}

type UpdateTaskBatchReq struct {
	IDs    []uint            `json:"ids" binding:"required"`
	Status *model.TaskStatus `json:"status"`
	Public *bool             `json:"public"`
}

type DelTaskReq struct {
	ID uint `json:"id" binding:"required"`
}

type CreateTaskReq struct {
	Title   string            `json:"title" binding:"required,max=128"`
	Content string            `json:"content"`
	Status  *model.TaskStatus `json:"status"`
	Public  bool              `json:"public"`
}

type TaskRespData struct {
	ID       uint             `json:"id"`
	Title    string           `json:"title"`
	Status   model.TaskStatus `json:"status"`
	Public   bool             `json:"public"`
	CreateAt string           `json:"createAt"`
	Content  string           `json:"content"`
}

type TaskRespDetail struct {
	TaskRespData
	CreatedTime string `json:"createdTime"`
	StartTime   string `json:"startTime"`
	EndTime     string `json:"endTime"`
	AbandonTime string `json:"abandonTime"`
}

func Models2Tasks(tasks []model.Task) []TaskRespData {
	var list []TaskRespData
	for _, t := range tasks {
		list = append(list, TaskRespData{
			ID:       t.ID,
			Title:    t.Title,
			Status:   t.Status,
			Public:   t.Public,
			Content:  t.Content,
			CreateAt: t.CreatedAt.Format(time.DateTime),
		})
	}
	return list
}

func Model2TaskDetail(task model.Task) (detail *TaskRespDetail) {
	detail = &TaskRespDetail{}
	detail.ID = task.ID
	detail.Title = task.Title
	detail.Status = task.Status
	detail.Public = task.Public
	detail.Content = task.Content
	detail.CreatedTime = task.CreatedAt.Format(time.DateTime)
	if task.StartTime != nil {
		detail.StartTime = task.StartTime.Format(time.DateTime)
	}
	if task.EndTime != nil {
		detail.EndTime = task.EndTime.Format(time.DateTime)
	}
	if task.AbandonTime != nil {
		detail.AbandonTime = task.AbandonTime.Format(time.DateTime)
	}
	return
}
